package tests;

import common.Utils;
import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.time.Duration;

public class RegisterPage extends Utils {
    public static String url = "http://49.235.61.184:8080/user/login";
    public RegisterPage() {
        super(url);
    }
    //检查点击注册，出现页面是否可见
    public void checkRegisterPage() {
        //点击注册按钮
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div:nth-child(4) > a")).click();
        //判断是否进入注册页面
        //出现注册账户，则证明到达注册页面
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));
        System.out.println("到达注册页面");

        //检查注册页的元素
        //检查姓名提示是否显示
       String name = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(2) > div > div.ant-col.ant-form-item-label > label")).getText();
       //校验
        assert name.equals("姓名");

        //检查姓名输入框是否可见
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(2) > div > div.ant-col.ant-form-item-control > div > div > span"));

        //检查登录账号提示字段是否显示
        String account = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(3) > div > div.ant-col.ant-form-item-label")).getText();
         //校验
        assert account.equals("登录账号");
        //检查登录账号输入框是否可见
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(3) > div > div.ant-col.ant-form-item-control > div > div > span"));

        //检查密码提示字段是否显示
        String password = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(4) > div > div.ant-col.ant-form-item-label > label")).getText();
        //校验
        assert password.equals("密码");
        //检查密码输入框是否可见
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(4) > div > div.ant-col.ant-form-item-control > div > div > span"));

        //检查密码确认提示字段是否显示
        String confirmPassword = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(5) > div > div.ant-col.ant-form-item-label > label")).getText();
        //校验
        assert confirmPassword.equals("确认密码");
        //检查密码确认输入框是否可见
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(5) > div > div.ant-col.ant-form-item-control > div > div > span"));

        //检查返回登录字段是否可见
        String Return_to_Login = driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div:nth-child(6) > a")).getText();
        //校验
        assert Return_to_Login.equals("返回登录");

        System.out.println("注册页面检查完毕");
    }
    //异常注册+正确登录测试
    public void registerException() throws InterruptedException {
        //输入姓名但不输入密码、确认密码、登录账号
        driver.findElement(By.cssSelector("#name")).sendKeys("Test");
        System.out.println("输入姓名但不输入密码、确认密码、登录账号");
        //点击注册按钮
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();
        //判断是否出现错误提示
        //如果还是出现注册登录按钮，就证明没有注册成功
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));
        System.out.println("缺少参数：密码、确认密码、登录账号");

        //输入登录账号、密码、确认密码
        driver.findElement(By.cssSelector("#username")).sendKeys("test");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#rePassword")).sendKeys("123456");
        //点击注册按钮
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();
        //出现账户已存在的提示
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button"));
        System.out.println("账户已存在,无法注册");
        //清除原来的账号
        //点击登录账号输入框
        driver.findElement(By.cssSelector("#username")).click();
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#username")).clear();
        //输入新的登录账号
        //使用随机生成字符串作为账号
        String account = RandomStringUtils.randomAlphanumeric(8);
        driver.findElement(By.cssSelector("#username")).sendKeys(account);
        //点击注册按钮
        driver.findElement(By.cssSelector("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")).click();
        //出现注册成功的提示 并跳转到登录页面
        //如果登录页面中的登录账号按钮可见，则证明注册成功
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button"));
        System.out.println("注册成功，成功进入登录页面");

        //正确登录测试
        driver.findElement(By.cssSelector("#username")).sendKeys("lzy");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        //点击登录按钮
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();
        //判断是否进入列表页
        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > aside > div > div:nth-child(1) > ul > li.ant-menu-item.ant-menu-item-selected.ant-menu-item-only-child.ant-pro-base-menu-menu-item"));
        System.out.println("登录成功，成功进入列表页");
        //存储url
        detailUrl = driver.getCurrentUrl();
    }

}
